package org.example.BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class Find_the_maximum_value_in_each_tree_row {
    public static List<Integer> arr = new ArrayList<>();
    public static void main(String[] args) {
        //在每个树行中找最大值

        //给定一棵二叉树的根节点 root ，请找出该二叉树中每一层的最大值。
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode root = new TreeNode(0);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node5.left = node7;
        get(root,0);
        for (Integer i : arr) {
            System.out.print(i+" ");
        }
    }
    public static void get(TreeNode root,int deep) {
        if (root==null) return;
        deep++;
        if (arr.size()<deep){
            arr.add(root.val);
        }else{
            Integer num = arr.get(deep - 1);
            if (num<root.val){
                arr.set(deep-1, root.val);
            }
        }
        get(root.left,deep);
        get(root.right,deep);
    }

}
